package club.weiyuanstudio.android.app.message_station.ui.home

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import club.weiyuanstudio.android.app.message_station.R
import club.weiyuanstudio.android.app.message_station.api.data.Node
import java.time.Instant
import java.util.*

class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_node_list, container, false)

        // Set the adapter
        val recycleView = view.findViewById<RecyclerView>(R.id.recycleNodeList)
        if (view.findViewById<RecyclerView>(R.id.recycleNodeList) is RecyclerView) {
            with(recycleView) {
                layoutManager = LinearLayoutManager(context)
                adapter =
                    NodeRecyclerViewAdapter(
                        arrayOf(
                            Node(
                                "1",
                                "test",
                                98,
                                "4G",
                                Date.from(Instant.now())
                            )
                        )
                    )
            }
        }

        val refreshList = view.findViewById<SwipeRefreshLayout>(R.id.swipeRefreshNodeList)

        refreshList.setOnRefreshListener {
            Toast.makeText(context, "Refresh Success", Toast.LENGTH_SHORT).show()
            refreshList.isRefreshing = false
        }

        return view
    }
}
